using System.Drawing;
using LowLevelGraphics.Types.Interval;

namespace LowLevelGraphics.Analysis
{
    /// <summary>
    /// Pixel Classifier, use this to classify pixels in a certain way. 
    /// </summary>
    public class Classify
    {
        /// <summary>
        /// Takes a pixel grey-level and an array of classifications
        /// returns a value between 1 and the number of classifications
        /// if the pixel value falls inside a classification and 0 if
        /// the pixel cannot be classified.
        /// </summary>
        /// <param name="pix">the grey-level value to be classified</param>
        /// <param name="classification">the array of high and low limits for classifications</param>
        /// <returns>the pixel classification (0 is unclassified)</returns>
        public int ClassifyPixel(int pix, IntInterval[] classification)
        {
            for (int i = 0; i < classification.Length; ++i)
            {
                if ((pix >= classification[i].Min) &&
               (pix <= classification[i].Max))
                {
                    return i + 1;   //classify pixel as array location (start at 1)
                }
            }
            return 0;   //if pixel does not fit any classification
        }

        /// <summary>
        /// Classifies each pixel in the input array according to the
        /// array of classifications (high/low limits).
        /// </summary>
        /// <param name="input">the input image</param>
        /// <param name="width">width of the input image</param>
        /// <param name="height">height of the input image</param>
        /// <param name="classification">the output classification image</param>
        /// <returns></returns>
        public int[,] ClassifyImage(UnsafeBitmap input, int width, int height,
                        IntInterval[] classification)
        {
            int[,] aOutput = new int[width, height];
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    int pixel = ColorFunctions.Gray(input[x, y]); //get pixel grey level
                    aOutput[x, y] = ClassifyPixel(pixel, classification);
                }
            }
            return aOutput;
        }
    }
}


